package com.zznj.xzjc.fragment;

import android.graphics.Typeface;
import android.view.View;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.vondear.rxui.fragment.NewLazyFragment;
import com.zznj.xzjc.R;
import com.zznj.xzjc.fragment.index.PushFragment;
import com.zznj.xzjc.view.StatusBarUtil;
import com.zznj.xzjc.widget.FragmentAda;
import com.zznj.xzjc.widget.MyViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/3
 */
public class PushMainFragment extends NewLazyFragment {
    Unbinder unbinder;

    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.main_viewpager)
    MyViewPager mViewPager;

    @BindView(R.id.titleLinear)
    LinearLayout titleLinear;


    private String[] mTitle = new String[]{"未读", "已读"};
    private ArrayList<Fragment> fragList = new ArrayList<Fragment>();
    private int selectIndex = 0;

    @Override
    protected int getContentViewId() {
        return R.layout.gyss_fragment_push_layout;
    }

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
        init();
        super.initView(view);
    }

    @Override
    public void onResume() {
        super.onResume();
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
    }

    private void init() {
        fragList.clear();
        fragList.add(PushFragment.getInstance(1));
        fragList.add(PushFragment.getInstance(2));

        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mViewPager.setAdapter(new FragmentAda(getChildFragmentManager(), fragList,null));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.setViewPager(mViewPager, mTitle);

        mViewPager.setCurrentItem(0);
        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    @Override
    protected void initData() {

    }

}
